<template>
  <div class="w-full h-full ttc">
    <div v-if="currentView === 1" class="track-order">
      <div class="back-bar">
        <div class="flex gap-2 items-center cursor-pointer" @click="tostore">
          <svg xmlns="http://www.w3.org/2000/svg" class="inline-block h-8" width="24" height="24" fill="currentColor" viewBox="0 0 16 16">
            <path fill-rule="evenodd" d="M15 8a.5.5 0 0 0-.5-.5H2.707l3.147-3.146a.5.5 0 1 0-.708-.708l-4 4a.5.5 0 0 0 0 .708l4 4a.5.5 0 0 0 .708-.708L2.707 8.5H14.5A.5.5 0 0 0 15 8z" />
          </svg>
          <b>Back to store</b>
        </div>
      </div>
      <!-- 返回按钮 -->
      <!-- <div class="back-button">
        <router-link to="/store" class="back-link">
          <i class="fas fa-chevron-left"></i>
          Back to store
        </router-link>
      </div> -->

      <!-- 主要内容区域 -->
      <div class="content">
        <h1 class="title text-3xl" style="color: white; font-weight: bold">TRACK MY ORDER</h1>

        <div class="order-form">
          <!-- Logo -->
          <div class="logo">
            <img :src="assets.logo" alt="GentleBands Logo" class="inline-block h-12 max-w-full" />
          </div>

          <!-- 表单 -->
          <form @submit.prevent="searchOrder">
            <div class="form-group">
              <span class="text-4">Please enter the email address you used when placing the order</span>
              <label for="email" class="required text-black text-4">Email address</label>
              <input type="email" id="email" v-model="email" required placeholder="Enter your email" />
            </div>
            <button type="submit" class="search-btn" :disabled="loading">
              <svg v-if="loading" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50" class="loading-spinner">
                <circle cx="25" cy="25" r="20" fill="none" stroke="white" stroke-width="5" stroke-dasharray="80,30" />
              </svg>
              <span v-else>SEARCH</span>
            </button>
          </form>
        </div>
      </div>
    </div>
    <div v-else-if="currentView === 2" class="track-detail bg-[#f7f7f7]">
      <div class="back-bar sticky">
        <div class="flex gap-2 items-center cursor-pointer justify-left" style="justify-content: space-between" @click="currentView = 1">
          <svg xmlns="http://www.w3.org/2000/svg" class="inline-block h-8" width="24" height="24" fill="currentColor" viewBox="0 0 16 16">
            <path fill-rule="evenodd" d="M15 8a.5.5 0 0 0-.5-.5H2.707l3.147-3.146a.5.5 0 1 0-.708-.708l-4 4a.5.5 0 0 0 0 .708l4 4a.5.5 0 0 0 .708-.708L2.707 8.5H14.5A.5.5 0 0 0 15 8z" />
          </svg>
          <b class="margin:0 auto;">TRACK MY ORDER</b>
          <div style="width: 24px"></div>
        </div>
      </div>
      <!-- 顶部进度指示器 -->
      <div class="progress-tracker wule" v-if="searchData.length === 1">
        <div class="progress-step" :class="{ active: data[0].status >= 1 }">
          <div class="icon">
            <img :src="assets.oneAc" alt="处理中" />
          </div>
          <div class="label">PROCESSING</div>
        </div>
        <div style="font-size: 3em">······</div>
        <div class="progress-step" :class="{ active: data[0].status >= 2 }">
          <div class="icon">
            <img v-if="data[0].status >= 2" :src="assets.twoAc" alt="运输中" />
            <img v-else :src="assets.two" alt="运输中" />
          </div>
          <div class="label">SHIPPING</div>
        </div>
        <div style="font-size: 3em">······</div>
        <div class="progress-step" :class="{ active: data[0].status >= 3 }">
          <div class="icon">
            <img v-if="data[0].status >= 3" :src="assets.threeAc" alt="已送达" />
            <img v-else :src="assets.three" alt="已送达" />
          </div>
          <div class="label">DELIVERED</div>
        </div>
      </div>
      <div v-else-if="data.length > 1" class="leading-[6] text-center">Your order contains {{ data.length }} packages.</div>

      <div class="bottom h-fit bg-white mb-4" v-for="(item, index) in data" :key="item.id" v-show="showMore || index === 0">
        <div class="order-info">
          <!--
          <img width="30" :src="assets.kuaidi" />
          <div class="tracking-number">{{ item.order_id }}</div>
          <a href="#">Contact USPS</a>
          -->
          <div v-if="item.status === 1">Your order has been placed.</div>
          <div v-else-if="item.status === 2">Your order is being shipped.</div>
          <div v-else-if="item.status === 3">Your order is delivered.</div>
        </div>

        <div class="divide-line"></div>

        <div class="timeline">
          <div v-for="(event, index) in item.list" :key="event.status" class="timeline-item" :class="{ active: index === 0 }">
            <div class="timeline-point">
              <svg v-if="item.status === 3" xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-check-lg" viewBox="0 0 16 16">
                <path
                  d="M12.736 3.97a.733.733 0 0 1 1.047 0c.286.289.29.756.01 1.05L7.88 12.01a.733.733 0 0 1-1.065.02L3.217 8.384a.757.757 0 0 1 0-1.06.733.733 0 0 1 1.047 0l3.052 3.093 5.4-6.425z"
                />
              </svg>
            </div>
            <div class="timeline-content">
              <!-- <div class="event-time">{{ event.time }}</div> -->
              <div v-if="event.status" class="event-location" style="font-weight: 500; color: black">{{ event.status }}</div>
              <div class="event-description">{{ event.time }}</div>
            </div>
          </div>
        </div>
      </div>

      <div class="text-center mb-6" v-if="searchData.length > 1">
        <span class="inline-block text-sky-500 cursor-pointer" v-if="showMore" @click="showMore = false">
          Show less
          <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="inline" viewBox="0 0 16 16">
            <path fill-rule="evenodd" d="M7.646 4.646a.5.5 0 0 1 .708 0l6 6a.5.5 0 0 1-.708.708L8 5.707l-5.646 5.647a.5.5 0 0 1-.708-.708l6-6z" />
          </svg>
        </span>
        <span class="inline-block text-sky-500 cursor-pointer" v-else @click="showMore = true">
          Show tracking for all packages
          <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="inline" viewBox="0 0 16 16">
            <path fill-rule="evenodd" d="M1.646 4.646a.5.5 0 0 1 .708 0L8 10.293l5.646-5.647a.5.5 0 0 1 .708.708l-6 6a.5.5 0 0 1-.708 0l-6-6a.5.5 0 0 1 0-.708z" />
          </svg>
        </span>
      </div>

      <div v-show="searchData.length === 0" style="margin: auto; text-align: center; width: 20%; max-width: 200px">
        <svg style="width: 100%" viewBox="0 0 64 41" xmlns="http://www.w3.org/2000/svg">
          <g transform="translate(0 1)" fill="none" fill-rule="evenodd">
            <ellipse fill="#f5f5f5" cx="32" cy="33" rx="32" ry="7"></ellipse>
            <g fill-rule="nonzero" stroke="#d9d9d9">
              <path d="M55 12.76L44.854 1.258C44.367.474 43.656 0 42.907 0H21.093c-.749 0-1.46.474-1.947 1.257L9 12.761V22h46v-9.24z"></path>
              <path
                d="M41.613 15.931c0-1.605.994-2.93 2.227-2.931H55v18.137C55 33.26 53.68 35 52.05 35h-40.1C10.32 35 9 33.259 9 31.137V13h11.16c1.233 0 2.227 1.323 2.227 2.928v.022c0 1.605 1.005 2.901 2.237 2.901h14.752c1.232 0 2.237-1.308 2.237-2.913v-.007z"
                fill="#fafafa"
              ></path>
            </g>
          </g>
        </svg>
        <span style="color: darkgray">No data</span>
      </div>
    </div>
    <div v-else style="height: 100vh; width: 100%; display: flex; align-items: center; justify-content: center">
      <svg
        viewBox="0 0 1024 1024"
        style="width: 20vw; max-width: 50px; transition: transform 0.3s ease-in-out; animation: loadingCircle 1s infinite linear"
        focusable="false"
        data-icon="loading"
        fill="currentColor"
        aria-hidden="true"
      >
        <path
          d="M988 548c-19.9 0-36-16.1-36-36 0-59.4-11.6-117-34.6-171.3a440.45 440.45 0 00-94.3-139.9 437.71 437.71 0 00-139.9-94.3C629 83.6 571.4 72 512 72c-19.9 0-36-16.1-36-36s16.1-36 36-36c69.1 0 136.2 13.5 199.3 40.3C772.3 66 827 103 874 150c47 47 83.9 101.8 109.7 162.7 26.7 63.1 40.2 130.2 40.2 199.3.1 19.9-16 36-35.9 36z"
        ></path>
      </svg>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onActivated, computed } from "vue";

const currentView = ref(0);
const email = ref("");
const loading = ref(false);
const searchData = ref([]);
const showMore = ref(false);

const assets = {
  logo: "https://d24r3siuq13q9r.cloudfront.net/202504/b7f6b03a-609c-4a69-94d1-4026e9f63457.png",
  one: "https://d24r3siuq13q9r.cloudfront.net/202504/4d59d08e-1260-43d6-89fc-d7956abe6138.png",
  oneAc: "https://d24r3siuq13q9r.cloudfront.net/202504/ade7f85a-f2d5-473a-abc2-63d40cffd210.png",
  two: "https://d24r3siuq13q9r.cloudfront.net/202504/3287f624-8b9a-43dc-b086-0d35421a9a06.png",
  twoAc: "https://d24r3siuq13q9r.cloudfront.net/202504/03877c61-4de7-438a-9a91-3dbecfd443db.png",
  three: "https://d24r3siuq13q9r.cloudfront.net/202504/b1b70541-8bcf-44f5-a1e0-582828ed21d9.png",
  threeAc: "https://d24r3siuq13q9r.cloudfront.net/202504/7157b4f1-26bc-42b7-ba9d-3a6d43de1767.png",
  kuaidi: "https://tts2.gentlebands.com/wp-content/uploads/2025/04/v2_suc3y2.png",
};

const data = computed(() => {
  return searchData.value.map((i) => {
    const rt = { id: i.order_id, list: i.logistics.toReversed() };
    let status = 1;
    if (rt.list[0]?.status.includes("DELIVERED")) status = 3;
    else if (rt.list.length >= 4) status = 2;
    rt.status = status;
    return rt;
  });
});
const tostore = () => {
  window.top.location.href = "/product-category/best-sellers/";
};
const searchOrder = () => {
  showMore.value = false;
  loading.value = true;

  fetch("https://tts.gentlebands.com/wp-json/lpapi/v1/exchange/getLogistics", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ email: email.value }),
  })
    .then((rsp) => rsp.json())
    .then((rsp) => {
      if (rsp.success) {
        searchData.value = rsp.data;
        // searchData.value = [
        //   {
        //     logistics: [
        //       {
        //         time: "2024-10-22 18:09",
        //         status: "Your order has been placed.",
        //       },
        //       {
        //         time: "2024-10-22 18:09",
        //         status: "We're currently preparing your item.",
        //       },
        //       {
        //         time: "2024-10-26 15:16",
        //         status: "Your ring is being crafted by our artisan.",
        //       },
        //       {
        //         time: "2024-10-27 21:17",
        //         status: "Your order is being packaged and awaiting shipment.",
        //       },
        //       {
        //         time: "2024-11-02 01:15",
        //         status: "Shipping Label Created,YodelAwaiting Item",
        //       },
        //       {
        //         time: "2024-11-03 09:21",
        //         status: "Picked Up By Shipping Partner,YodelAwaiting Item",
        //       },
        //       {
        //         time: "2024-11-05 11:01",
        //         status: "Arrived at Shipping Partner Facility,YodelAwaiting Item",
        //       },
        //       {
        //         time: "2024-11-06 23:25",
        //         status: "Accepted at YodelOrigin Facility",
        //       },
        //       {
        //         time: "2024-11-08 14:15",
        //         status: "Arrived at YodelRegional Origin Facility",
        //       },
        //       {
        //         time: "2024-11-10 17:57",
        //         status: "In transit to Next Facility",
        //       },
        //       {
        //         time: "2024-11-12 13:46",
        //         status: "Arrived at YodelRegional Facility",
        //       },
        //       {
        //         time: "2024-11-12 23:12",
        //         status: "Departed YodelRegional Facility",
        //       },
        //       {
        //         time: "2024-11-13 22:18",
        //         status: "Arrived at Post Office",
        //       },
        //       {
        //         time: "2024-11-14 14:29",
        //         status: "Out for Delivery",
        //       },
        //       {
        //         time: "2024-11-18 03:41",
        //         status: "DELIVERED: 2025-04-24 09:53 AMARILLO, TX 79124, Delivered -> Your item was delivered on April 24, 2025 in AMARILLO, TX 79124.",
        //       },
        //     ],
        //     order_id: "211708",
        //     logicsWayName: "深圳燕文-专线追踪小包-普货",
        //   },
        // ];
        currentView.value = 2;
      }
    })
    .catch(() => {
      searchData.value = [];
    })
    .finally(() => {
      loading.value = false;
    });
};
onMounted(() => {
  try {
    if (logistics_order_info) {
      searchData.value = logistics_order_info;
      setTimeout(() => {
        currentView.value = 2;
      }, 2500);
      return;
    }
  } catch (e) {
    setTimeout(() => {
      currentView.value = 1;
    }, 500);
  }
});
</script>

<style>
.ttc {
  font-size: clamp(12px, 3.8vw, 18px);
}
.track-order {
  position: relative;
  min-height: 100vh;
  background-color: #000;
  color: #fff;
  padding-top: 10vh;
  box-sizing: border-box;
}

.track-order .back-bar {
  position: absolute;
  top: 0;
  font-size: 18px;
  line-height: 3;
  padding: 0 20px;
  background-color: black;
  color: white;
}

.track-order .required::after {
  content: "*";
  color: red;
  margin-left: 6px;
}
.track-order .back-button {
  margin-bottom: 30px;
}
.track-order .order-item {
  line-height: 1;
  cursor: pointer;
  text-align: center;
  margin-top: 1rem;
}
.track-order .back-link {
  color: #fff;
  text-decoration: none;
  display: flex;
  align-items: center;
  gap: 8px;
}
.track-order .content {
  max-width: 800px;
  margin: 0 auto;
  padding: max(46px, 1rem) 2rem;
}

.track-order .title {
  text-align: center;
  margin-bottom: 40px;
}

.track-order .order-form {
  background-color: #fff;
  border-radius: 8px;
  padding: 30px;
  color: #000;
}

.track-order .logo {
  text-align: center;
  margin-bottom: 30px;
}

.track-order label {
  display: block;
  margin-bottom: 10px;
  margin-top: 20px;
  font-weight: bold;
}

.track-order input {
  width: 100%;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  margin-bottom: 20px;
}

.track-order .search-btn {
  width: 100%;
  height: 3.6em;
  line-height: 3.6em;
  background-color: #e28601;
  border: none;
  border-radius: 4px;
  color: white;
  font-weight: bold;
  cursor: pointer;
  transition: background-color 0.3s;
}

.track-order .search-btn:hover {
  background-color: #e28601dd;
}

.track-order .search-btn[disabled] {
  background: #bbb;
}

/* page2 */
.track-detail {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  max-width: 800px;
  margin: 0 auto;
}

.track-detail .sticky {
  z-index: 100;
  position: sticky;
  top: 0;
}

.track-detail .back-bar {
  font-size: 18px;
  line-height: 3;
  padding: 0 20px;
  background-color: black;
  color: white;
}

.track-detail .progress-tracker {
  display: flex;
  justify-content: space-between;
  padding: 1.2rem;
  white-space: nowrap;
}
.track-detail .progress-tracker .progress-step {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
  color: #999;
}

.track-detail .progress-tracker .progress-step .icon {
  border-radius: 50%;
  margin-bottom: 8px;
  text-align: center;
}

.track-detail .progress-tracker .progress-step .icon img {
  display: inline-block;
  object-fit: contain;
  width: 60%;
}
.track-detail .progress-tracker .progress-step .icon img.xl {
  width: 3.6rem;
}

.track-detail .bottom {
  border: 3px solid lightgray;
  padding: 20px;
}

.track-detail .bottom .order-info {
  display: flex;
  align-items: center;
}

.track-detail .bottom .order-info a {
  color: black;
  font-weight: bolder;
}

.track-detail .bottom .order-info .tracking-number {
  color: #333;
  margin: 0 auto 0 10px;
}

.track-detail .bottom .divide-line {
  margin: 20px 0;
  border-top: 2px solid lightgray;
}

.track-detail .bottom .timeline {
  display: flex;
  flex-direction: column;
}

.timeline-item {
  position: relative;
  padding-left: 2rem;
  padding-bottom: 2rem;
  line-height: 1.2rem;
}

.timeline-item.active .timeline-point {
  background: #ff6b00;
}

.timeline-item::before {
  content: "";
  position: absolute;
  left: 0.5rem;
  top: 1rem;
  bottom: 0;
  width: 0.2rem;
  background: #ff6b00;
}

.timeline-item:last-child::before {
  display: none;
}

.timeline-item .timeline-point {
  position: absolute;
  left: 0;
  width: 1.2rem;
  height: 1.2rem;
  font-size: 1rem;
  border-radius: 50%;
  background: white;
  color: white;
  border: 0.2rem solid #ff6b00;
}

.timeline-item .timeline-point svg {
  width: 100%;
  height: 100%;
  vertical-align: top;
}

.timeline-item .timeline-content {
  padding: 0;
  border-radius: 8px;
}

.timeline-item .timeline-content .event-time {
  line-height: 1;
  font-size: 14px;
  color: #666;
  margin-bottom: 5px;
}

.timeline-item .timeline-content .event-location {
  font-weight: bold;
  margin-bottom: 5px;
}

.timeline-item .timeline-content .event-description {
  color: #666;
}

.loading-spinner {
  animation: spin 1s linear infinite;
  height: 1.5em;
  display: inline-block;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

@keyframes loadingCircle {
  100% {
    -webkit-transform: rotate(360deg);
    transform: rotate(360deg);
  }
}

@media (max-width: 500px) {
  .wule {
    font-size: 10px;
  }
}
</style>
